package com.zeng.niceservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecore.constant.IgnoreConstant;
import com.zeng.nicecore.util.BeanUtil;
import com.zeng.nicecore.util.IgnoreStringUtil;
import com.zeng.nicedao.mapper.FileRecordMapper;
import com.zeng.nicepojo.entity.FileRecord;
import com.zeng.nicepojo.vo.FileRecordVo;
import com.zeng.niceservice.service.FileRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class FileRecordServiceImpl implements FileRecordService {

    @Autowired
    FileRecordMapper fileRecordMapper;

    @Override
    public FileRecordVo addOne(FileRecordVo fileRecordVo) {
    FileRecord fileRecord = FileRecord.builder().build();
    BeanUtils.copyProperties(fileRecordVo, fileRecord, IgnoreStringUtil.ignoreExclude());
    int insert = fileRecordMapper.insert(fileRecord);
    return BeanUtil.beanToVo(fileRecord, fileRecordVo);
    }

    @Override
    public FileRecordVo getOneByCode(String code) {

    FileRecord fileRecord = this.fileRecordMapper.selectById(code);
    FileRecordVo fileRecordVo = BeanUtil.beanToVo(fileRecord, new FileRecordVo());

    return fileRecordVo;
    }

    @Override
    public Integer update(FileRecordVo fileRecordVo) {
    FileRecord fileRecord = BeanUtil.VoToBean(fileRecordVo,
    new FileRecord(),
    IgnoreStringUtil.ignoreExclude(IgnoreConstant.code.getValue()));
    return this.fileRecordMapper.updateById(fileRecord);
    }

    @Override
    public Integer delete(String code) {
    return this.fileRecordMapper.deleteById(code);
    }

    @Override
    public Integer batchDelete(List<String> codes) {

    return this.fileRecordMapper.deleteBatchIds(codes);
    }

    @Override
    public List<FileRecordVo> findPage(Page page, FileRecordVo fileRecordVo) {
        FileRecord fileRecord = BeanUtil.VoToBean(fileRecordVo, new FileRecord());
        this.fileRecordMapper.selectPage(page, this.getWrapper(fileRecordVo));
        List orders = page.getRecords();
        List<FileRecordVo> fileRecordVos = new ArrayList<>();
        orders.forEach(e->{
            fileRecordVos.add(BeanUtil.beanToVo(e, new FileRecordVo()));
        });
        page.setRecords(null);
        return fileRecordVos;
        }


    @Override
    public Integer count(FileRecordVo fileRecordVo) {
        Integer integer = this.fileRecordMapper.selectCount(this.getWrapper(fileRecordVo));
        return integer;
    }

    public AbstractWrapper getWrapper(FileRecordVo fileRecordVo) {
        QueryWrapper<FileRecord> wrapper = new QueryWrapper<>();
        if (fileRecordVo != null) {

                if (StringUtils.isNotEmpty(fileRecordVo.getName())) {
                    wrapper.like("name", fileRecordVo.getName());
                }
                if (StringUtils.isNotEmpty(fileRecordVo.getType())) {
                    wrapper.like("type", fileRecordVo.getType());
                }

        }
        return wrapper;
    }
}
